import { createSlice } from "@reduxjs/toolkit";
import { useSelector } from "react-redux";
import { useDispatch } from "react-redux/es/exports";
import { State, User } from "../../types";
import { RootSate } from "..";
const initialState:State = {user:null};
export const userSlice = createSlice({
    name:'userSlice',
    initialState,
    reducers: {
        userLogin: (state,action) => {
          state.user=action.payload;
        },
        userLogout: (state) => {
            state.user=null
            localStorage.removeItem("token")
        }
    }
})
export const useAuth = ()=>{
    const user:User|null = useSelector((state:RootSate)=>state.userReducer.user)
    const dispatch = useDispatch();
    const userLogin = (param:User) => dispatch(userSlice.actions.userLogin(param));
    const userLogout = () => dispatch(userSlice.actions.userLogout());
    return {user,userLogin,userLogout}
}
